package com.soecode.lyf.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.soecode.lyf.entity.PersonRecord;

public class ExcelExport {
	public HSSFWorkbook exportNoResponse(String sheetName, int columnNumber, String[] columnName, List dataList)
			throws Exception {
		// 创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 创建第1行 也就是表头
		HSSFRow row = sheet.createRow((int) 0);

		// 创建表头的列
		for (int i = 0; i < columnNumber; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(columnName[i]);
		}

		// 创建单元格，并设置值
		for (int i = 0; i < dataList.size(); i++) {
			Object ob = dataList.get(i);
			Field[] field = ob.getClass().getDeclaredFields();
			row = sheet.createRow((int) i + 1);
			HSSFCell datacell = null;
			for (int j = 0; j < columnNumber; j++) {
				datacell = row.createCell(j);
				String name = field[j].getName(); // 获取属性的名字
				name = name.substring(0, 1).toUpperCase() + name.substring(1);
				Method getMethod = ob.getClass().getDeclaredMethod("get" + name);
				Object value = getMethod.invoke(ob, new Object[] {});
				if (value != null) {
					datacell.setCellValue(value.toString());
				}

				// if (value instanceof Boolean) {
				// boolean bValue = (Boolean) value;
				// datacell.setCellValue(bValue);
				// }else if(value instanceof Date) {
				// Date date = (Date) value;
				// datacell.setCellValue(date);
				// } else{
				// textValue = value.toString();
				// }

			}
		}
		return wb;
	}

	/*
	 * 根据模板导出excel
	 */
	public HSSFWorkbook exportResponse(String sheetName, int columnNumber, String[] columnName, List dataList)
			throws Exception {
		Map<PersonRecord, List<PersonRecord>> map = Maps.newHashMap();
		int maxHSSFCell = getMaxHSSFCell(map, dataList);
		// 创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 创建第1行 也就是表头
		HSSFRow row = sheet.createRow((int) 0);

		// 创建表头的列
		for (int i = 0; i < columnNumber; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(columnName[i]);
		}
		for (int i = 1; i < maxHSSFCell; i++) {
			HSSFCell cell = row.createCell(columnNumber++);
			cell.setCellValue("门号");
			cell = row.createCell(columnNumber++);
			cell.setCellValue("时间");
		}

		int rowNumber = 1;
		for (Map.Entry<PersonRecord, List<PersonRecord>> entry : map.entrySet()) {
			PersonRecord personRecord = entry.getKey();
			row = sheet.createRow(rowNumber);
			HSSFCell datacell = row.createCell(0);
			if (personRecord.getICNO() != null) {
				datacell.setCellValue(personRecord.getICNO());
			}

			datacell = row.createCell(1);
			if (personRecord.getIDNO() != null) {
				datacell.setCellValue(personRecord.getIDNO());
			}

			datacell = row.createCell(2);
			if (personRecord.getNO() != null) {
				datacell.setCellValue(personRecord.getNO());
			}

			datacell = row.createCell(3);
			if (personRecord.getName() != null) {
				datacell.setCellValue(personRecord.getName());
			}

			datacell = row.createCell(4);
			if (personRecord.getAreaID() != null) {
				datacell.setCellValue(personRecord.getAreaID());
			}

			datacell = row.createCell(5);
			if (personRecord.getSex() != null) {
				datacell.setCellValue(personRecord.getSex());
			}

			datacell = row.createCell(6);
			if (personRecord.getIdentityNo() != null) {
				datacell.setCellValue(personRecord.getIdentityNo());
			}

			int cellNumber = 7;
			for (PersonRecord p : entry.getValue()) {
				datacell = row.createCell(cellNumber++);
				if (p.getDoorName() != null) {
					datacell.setCellValue(p.getDoorName());
				}

				datacell = row.createCell(cellNumber++);
				if (p.getBrushCardTime() != null) {
					datacell.setCellValue(p.getBrushCardTime());
				}
			}
			rowNumber++;
		}
		return wb;
	}

	private int getMaxHSSFCell(Map<PersonRecord, List<PersonRecord>> map, List dataList) {
		int maxHSSFCell = 0;
		if (dataList != null && dataList.size() > 0) {
			List<PersonRecord> personRecordList = Lists.newArrayList();
			PersonRecord oldPersonRecord = (PersonRecord) dataList.get(0);
			int HSSFCellTemp = 0;
			for (Object ob : dataList) {
				PersonRecord personRecord = (PersonRecord) ob;
				if (personRecord.getNO() != null) {
					if (personRecord.getNO().equals(oldPersonRecord.getNO())) {
						personRecordList.add(personRecord);
						HSSFCellTemp++;
						continue;
					}
					map.put(oldPersonRecord, personRecordList);
					personRecordList = Lists.newArrayList();
					personRecordList.add(personRecord);
					oldPersonRecord = personRecord;
					if (maxHSSFCell < HSSFCellTemp) {
						maxHSSFCell = HSSFCellTemp;
					}
					HSSFCellTemp = 1;
				}
			}
			map.put(oldPersonRecord, personRecordList);
			if (maxHSSFCell < HSSFCellTemp) {
				maxHSSFCell = HSSFCellTemp;
			}
		}
		return maxHSSFCell;
	}
}
